﻿DECLARE @Var_CallNo          INT          SET @Var_CallNo      = @CallNo_In       
DECLARE @Var_CallName        VARCHAR(100) SET @Var_CallName    = @CallName_In     
DECLARE @Var_RegistId        VARCHAR(100) SET @Var_RegistId    = @RegistId_In     
DECLARE @Var_PayMoney        MONEY        SET @Var_PayMoney    = @PayMoney_In     
DECLARE @Var_PatientId       VARCHAR(100) SET @Var_PatientId   = @PatientId_In    
DECLARE @Var_TransNo         VARCHAR(100) SET @Var_TransNo     = @TransNo_In      
DECLARE @Var_PayWay          INT          SET @Var_PayWay      = @PayWay_In       
DECLARE @Var_BankTransNo     VARCHAR(100) SET @Var_BankTransNo = @BankTransNo_In  
DECLARE @Var_DeviceId        VARCHAR(100) SET @Var_DeviceId    = @PayDeviceId_In  
DECLARE @Var_YBFlag          INT          SET @Var_YBFlag      = @YBFlag_In

DECLARE @HIS_OperCode        VARCHAR(100)
DECLARE @HIS_RegFee          MONEY
DECLARE @HIS_ClinicFee       MONEY
DECLARE @HIS_DeptCode        VARCHAR(100)
DECLARE @HIS_DeptName        VARCHAR(100)
DECLARE @HIS_DoctCode        VARCHAR(100)
DECLARE @HIS_DoctName        VARCHAR(100) 
DECLARE @HIS_DoctTitleCode  VARCHAR(20)
DECLARE @HIS_DoctTitleName  VARCHAR(100)
DECLARE @HIS_TimeInterval    VARCHAR(100)
DECLARE @HIS_ReservationDate DATE
DECLARE @HIS_OrderDate       DATE
DECLARE @HIS_OrderTime       TIME(3)
  
DECLARE @HIS_CurrentDate            DATETIME
DECLARE @HIS_Spec                   VARCHAR(256)                                         

DECLARE @HIS_CheckGroupCode         VARCHAR(100)
DECLARE @HIS_CheckGroupName         VARCHAR(100)
DECLARE @HIS_CheckGroupPrice        MONEY   
                                            
DECLARE @HIS_PrePatientId           VARCHAR(100)
DECLARE @HIS_PatientName            VARCHAR(100)
DECLARE @HIS_PreRegistMoney         MONEY
DECLARE @HIS_PrePayMoney            MONEY
DECLARE @HIS_PreOperDate            DATETIME
DECLARE @HIS_IsFinish               VARCHAR(100)
DECLARE @HIS_SourceNumber           VARCHAR(100)
DECLARE @HIS_TransFlow              VARCHAR(100)
DECLARE @HIS_TransactionTime        VARCHAR(100)
DECLARE @HIS_CurrentNumber          VARCHAR(100)
DECLARE @HIS_QueueNumber            VARCHAR(100)
DECLARE @HIS_ClinicRange            VARCHAR(100)
DECLARE @HIS_Sex                    VARCHAR(100)
DECLARE @HIS_Address                VARCHAR(100) 
DECLARE @HIS_Duty                   VARCHAR(100)
DECLARE @HIS_AP_OrderSheet_Id       INT
  
SET @HIS_CurrentDate = GETDATE()

SELECT @HIS_DeptCode        = A.KSCode      
      ,@HIS_DeptName        = A.KSName
      ,@HIS_DoctCode        = A.YSCode
      ,@HIS_DoctName        = A.YSName
      ,@HIS_DoctTitleCode   = A.TitleCode
      ,@HIS_DoctTitleName   = A.TitleName
      ,@HIS_CheckGroupCode  = A.CheckGroupCode
      ,@HIS_CheckGroupName  = A.CheckGroupName
      ,@HIS_PrePatientId    = A.PatientId
      ,@HIS_PatientName     = A.PatientName   
      ,@HIS_TimeInterval    = A.TimeInterval
      ,@HIS_PreRegistMoney  = A.RegistMoney
      ,@HIS_PrePayMoney     = A.PayMoney
      ,@HIS_ReservationDate = A.RegistDate
      ,@HIS_PreOperDate     = A.PreOperDate
      ,@HIS_IsFinish        = A.IsFinish
      ,@HIS_SourceNumber    = A.SourceNumber   
      ,@HIS_TransFlow       = A.TransFlow      
      ,@HIS_TransactionTime = A.TransactionTime
      ,@HIS_CurrentNumber   = A.CurrentNumber  
      ,@HIS_QueueNumber     = A.QueueNumber    
      ,@HIS_ClinicRange     = A.ClinicRange    
      ,@HIS_Sex             = A.Sex               
      ,@HIS_Address         = A.Address          
      ,@HIS_Duty            = A.Duty                  
  FROM AP_PreRegist_DongRuan A(NOLOCK) 
 WHERE Id = @Var_RegistId AND A.IsReservation = 1

IF @HIS_DeptCode IS NULL
BEGIN
    SELECT -1 AS RESULT, '未找到相应的预挂号记录' AS RESULTTEXT
    RETURN
END

IF @HIS_IsFinish = 1 
BEGIN
    IF NOT EXISTS(SELECT * FROM CHECKGROUPSHEET (NOLOCK) WHERE GROUPCODE = @HIS_CheckGroupCode)
    BEGIN
        SELECT -1 AS RESULT, '未找到相应的费用明细' AS RESULTTEXT
        RETURN
    END
    SELECT 1 AS Result,'成功' AS ResultText,CHECKNAME,CHECKPRICE
           ,@HIS_ReservationDate AS AppointDate
           ,@HIS_SourceNumber    AS SourceNumber   
           ,@HIS_TransFlow       AS TransFlow      
           ,@HIS_TransactionTime AS TransactionTime
           ,@HIS_CurrentNumber   AS CurrentNumber  
           ,@HIS_QueueNumber     AS QueueNumber    
           ,@HIS_ClinicRange     AS ClinicRange    
           ,@HIS_PrePatientId    AS PatientId      
           ,@HIS_PatientName     AS Name           
           ,@HIS_Sex             AS Sex            
           ,@HIS_DeptName        AS DeptName       
           ,@HIS_Address         AS Address        
           ,@HIS_DoctName        AS DoctorName     
           ,@HIS_Duty            AS Duty                  
      FROM CHECKGROUPSHEET (NOLOCK) WHERE GROUPCODE=@HIS_CheckGroupCode
    RETURN
END
  
IF @HIS_PreOperDate < GETDATE()
BEGIN
    SELECT -1 AS RESULT,'预挂号已过期，请重新预挂号！' AS RESULTTEXT
    RETURN
END

IF @Var_PatientId <> @HIS_PrePatientId 
BEGIN
    SELECT -1 AS RESULT,'预挂号的PatientId和此次确认挂号的PatientId不匹配！' AS RESULTTEXT
    RETURN
END

IF @Var_PayMoney <> @HIS_PrePayMoney 
BEGIN
    SELECT -1 AS RESULT,'预挂号的金额与此次确认挂号的金额不匹配！' AS RESULTTEXT
    RETURN
END;

IF NOT EXISTS(SELECT * FROM CHECKGROUPCODE (NOLOCK) WHERE GROUPCODE=@HIS_CheckGroupCode AND GROUPPRICE=@Var_PayMoney)
BEGIN
    SELECT -1 AS RESULT,'未找到排班类型代码对应的挂号或传入的金额有误！' AS RESULTTEXT
    RETURN
END

SELECT @HIS_OperCode = CODE FROM YSCODE(NOLOCK) WHERE NAME = @Var_CallName
IF @HIS_OperCode IS NULL
BEGIN
  SELECT -1 AS RESULT,'未找到操作员代码，请联系开发商！' AS RESULTTEXT
  RETURN
END

SELECT @HIS_RegFee = CheckMoney1,
       @HIS_ClinicFee = CheckMoney2 
FROM dbo.MZSchedule(NOLOCK)
WHERE [Weekday] = DATEPART(WEEKDAY,@HIS_ReservationDate) AND KSCode = @HIS_DeptCode AND YSCode = @HIS_DoctCode


IF EXISTS(SELECT Id FROM AP_OrderSheet(NOLOCK) WHERE OrderFromType=@Var_CallNo AND (TRANSNO=@Var_TransNo or DELTRANSNO=@Var_TransNo))
BEGIN
  SELECT -1 AS RESULT,'TransNo重复！' AS RESULTTEXT
  RETURN
END

IF EXISTS(SELECT Id FROM AP_RESERVATIONSHEET(NOLOCK) WHERE PATIENTID=@Var_PatientId AND RESERVATIONDATE=@HIS_ReservationDate AND DELDATE IS NULL)
BEGIN
  SELECT -1 AS RESULT,'同一天内只能预约一次！' AS RESULTTEXT
  RETURN
END

declare @HIS_Totnum int
declare @HIS_Totnum2 int
declare @HIS_OtherAlreadyCount int

SELECT @HIS_Spec=SPECIALTY,@HIS_Totnum=ISNULL(totnum,0),@HIS_Totnum2=ISNULL(TOTNUM2,0) FROM MZSCHEDULE (NOLOCK) WHERE KSCODE=@HIS_DeptCode AND YSCODE=@HIS_DoctCode AND TIMEINTERVAL=@HIS_TimeInterval 
AND CHECKGROUPCODE=@HIS_CheckGroupCode AND 
(STATUS IS NULL OR STATUS=2) AND TIMEINTERVAL IS NOT NULL AND 
WEEKDAY=DATEPART(WEEKDAY,@HIS_ReservationDate)

IF @HIS_Totnum IS NULL
BEGIN
    SELECT -1 AS RESULT,'挂号目标不正确，请核对，有可能是医院调整了排班信息！' AS RESULTTEXT
    RETURN
END

SELECT @HIS_OtherAlreadyCount=COUNT(*) FROM AP_RESERVATIONSHEET(NOLOCK) 
WHERE DEPTCODE=@HIS_DeptCode AND DOCTCODE=@HIS_DoctCode AND DELDATE IS NULL AND RESERVATIONDATE=@HIS_ReservationDate

IF @HIS_TotNum > 0
BEGIN
  IF (@HIS_TotNum - @HIS_OtherAlreadyCount <= 0) OR (@HIS_OtherAlreadyCount >= @HIS_Totnum2)
  BEGIN
    SELECT -1 AS RESULT,'该医师号源已预约完了，请稍后再试！' AS RESULTTEXT
    RETURN
  END
END

IF (@HIS_DoctCode) = '*'
BEGIN
  SET @HIS_DoctCode = ''
  SET @HIS_DoctName = ''
  SET @HIS_CheckGroupCode  = '任意医师'
  SET @HIS_CheckGroupName = '任意医师'
END

INSERT INTO AP_ORDERSHEET
           ([TYPE]
           ,[DATE]
           ,[TIME]
           ,[MONEY]
           ,ORDERFROMTYPE
           ,ORDERFROMNAME
           ,OPERDATE
           ,TRANSNO
           ,DEVICEID
           ,BANKTRANSNO
           ,PayWay)
     VALUES
           (2
           ,@HIS_CurrentDate
           ,CAST(@HIS_CurrentDate AS TIME(0))
           ,@HIS_PrePayMoney
           ,@Var_CallNo
           ,@Var_CallName
           ,@HIS_CurrentDate
           ,@Var_TransNo
           ,@Var_DeviceId
           ,@Var_BankTransNo
           ,@Var_PayWay
       )
SELECT @HIS_AP_OrderSheet_Id = Id FROM dbo.AP_OrderSheet WHERE @@ROWCOUNT > 0 AND Id = scope_identity()         

INSERT INTO AP_RESERVATIONSHEET
           (PATIENTID
           ,OrderId
           ,DEPTCODE
           ,DEPTNAME
           ,DOCTCODE
           ,DOCTNAME
           ,TITLECODE
           ,TITLENAME
           ,SPEC
           ,TID
           ,CLINICCODE
           ,CLINICNAME
           ,CLINICFEE
           ,REGFEE
           ,TRANSNO
           ,RESERVATIONDATE
           ,RESERVATIONFROMTYPE
           ,RESERVATIONFROMNAME
           ,OPERDATE
           )
     Values(@Var_PatientId
           ,@HIS_AP_OrderSheet_Id
           ,@HIS_DeptCode
           ,@HIS_DeptName
           ,@HIS_DoctCode
           ,@HIS_DoctName
           ,@HIS_DoctTitleCode
           ,@HIS_DoctTitleName
           ,@HIS_Spec
           ,@HIS_TimeInterval
           ,@HIS_CheckGroupCode
           ,@HIS_CheckGroupName
           ,@HIS_ClinicFee
           ,@HIS_RegFee
           ,@Var_TransNo
           ,@HIS_ReservationDate
           ,@Var_CallNo
           ,@Var_CallName
           ,@HIS_CurrentDate)
           
UPDATE AP_PreRegist_DongRuan
SET sourceNumber    = CONVERT(VARCHAR(20),@HIS_AP_OrderSheet_Id)
   ,transFlow       = CONVERT(VARCHAR(20),@HIS_AP_OrderSheet_Id)
   ,transactionTime = @HIS_CurrentDate
   ,IsFinish        = 1
WHERE Id = @Var_RegistId AND IsFinish=0             


SELECT 1 AS Result,'成功' AS ResultText,CHECKNAME,CHECKPRICE
    ,@HIS_ReservationDate AS AppointDate
    ,CONVERT(VARCHAR(20),@HIS_AP_OrderSheet_Id) AS SourceNumber   
    ,CONVERT(VARCHAR(20),@HIS_AP_OrderSheet_Id) AS TransFlow      
    ,@HIS_CurrentDate AS TransactionTime
    ,NULL   AS CurrentNumber  
    ,NULL     AS QueueNumber    
    ,@HIS_TimeInterval     AS ClinicRange    
    ,@Var_PatientId    AS PatientId      
    ,@HIS_PatientName     AS Name           
    ,@HIS_Sex             AS Sex            
    ,@HIS_DeptName        AS DeptName       
    ,@HIS_Address         AS Address        
    ,@HIS_DoctName        AS DoctorName     
    ,@HIS_Duty            AS Duty                  
FROM CHECKGROUPSHEET (NOLOCK) WHERE GROUPCODE=@HIS_CheckGroupCode